import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'
import { TextareaControlSchema } from 'amis/lib/renderers/Form/Textarea'
import smsSupplierTypeSelectUtils from '@/components/sms/smsSupplierTypeSelectUtils'
import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import smsSupplierTypeColumnUtils from '@/components/sms/smsSupplierTypeColumnUtils'
import amisUtils from '@/components/amis/amisUtils'
import SmsSupplierTypeEnum from '@/core/sms/enums/SmsSupplierTypeEnum'
import { EditorControlSchema } from 'amis/lib/renderers/Form/Editor'
import smsAdminApiConfig from '@/core/sms/generated/apiconfig/smsAdminApiConfig'

const SmsSupplierList = () => {
	const entityName = '短信供应商'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = smsAdminApiConfig.smsSupplier

	const isAliyunExpression = amisUtils.getIsInValuesExpression('type', [SmsSupplierTypeEnum.ALIYUN.getCode()])
	const aliyunCommonProperties = {
		disabledOn: `!${isAliyunExpression}`,
		visibleOn: `${isAliyunExpression}`,
	}
	const aliyunFormItems = [
		{
			type: 'input-text',
			name: 'configJson.accessKeyId',
			label: 'accessKeyId',
			placeholder: 'AccessKeyId',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.accessKeySecret',
			label: 'AccessKeySecret',
			placeholder: 'AccessKeySecret',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.signature',
			label: 'Signature',
			placeholder: 'Signature,短信签名',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.templateId',
			label: '模板ID',
			placeholder: '模板ID',
			required: false,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.templateName',
			label: '模板变量名称',
			placeholder: '模板变量名称',
			required: false,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.requestUrl',
			label: '请求地址',
			placeholder: '请求地址,默认值：dysmsapi.aliyuncs.com',
			value: 'dysmsapi.aliyuncs.com',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.action',
			label: '接口名称',
			placeholder: '接口名称,默认值：SendSms',
			value: 'SendSms',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.action',
			label: '接口版本',
			placeholder: '接口版本,默认值：2017-05-25',
			value: '2017-05-25',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.regionId',
			label: '地域信息',
			placeholder: '地域信息,默认值：cn-hangzhou',
			value: 'cn-hangzhou',
			required: true,
			...aliyunCommonProperties,
		} as TextControlSchema,
	]

	const defaultCommonProperties = {
		disabledOn: `${isAliyunExpression}`,
		visibleOn: `!${isAliyunExpression}`,
	}

	const defaultFormItems = [
		{
			type: 'editor',
			name: 'configJson',
			label: '配置内容(json)',
			size: 'xxl',
			allowFullscreen: false,
			options: {
				automaticLayout: true,
			},
			language: 'json',
			required: false,
			...defaultCommonProperties,
		} as EditorControlSchema,
	]

	const configFormItems = [...aliyunFormItems, ...defaultFormItems]

	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'title',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'type',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: {
			apiConfig: modelApiConfig.add,
			data: {
				title: '${title}',
				remark: '${remark}',
				type: '${type}',
				config: '${configJson|json}',
			},
		},
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				id: '${id}',
				title: '${title}',
				remark: '${remark}',
				type: '${type}',
				config: '${configJson|json}',
			},
		} as DefaultCrudTableServerApi,
		debug: true,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				type: 'input-text',
				name: 'title',
				label: '名称',
				required: true,
				maxLength: 100,
			} as TextControlSchema,
			{
				type: 'textarea',
				name: 'remark',
				label: '备注',
				required: false,
				maxLength: 200,
				showCounter: true,
			} as TextareaControlSchema,
			{
				...smsSupplierTypeSelectUtils.getSchema({ name: 'type' }),
				required: true,
			} as SelectControlSchema,
			...configFormItems,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'ID',
				searchable: true,
			},
			{
				name: 'title',
				label: '名称',
				searchable: true,
			},
			{
				...smsSupplierTypeColumnUtils.getSchema(),
				searchable: smsSupplierTypeSelectUtils.getSchema(),
			},
			{
				type: 'json',
				name: 'config',
				levelExpand: 0,
				label: '短信组件配置(json)',
			},
			{
				name: 'remark',
				label: '备注',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default SmsSupplierList
